/*
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 */

package com.wttech.dzzf.process.dao;

import com.wtkj.dao.impl.ListResultSetExtractor;
import com.wttech.dzzf.process.model.user.FundAccount;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author MaShibo
 * @date 2017/12/2.
 */
@Repository
public class DataProcessFinalDao {

	@Resource(name = "jdbcTemplate")
	private JdbcTemplate testJdbcTemplate;

	@Resource(name = "dzzfJdbcTemplate")
	private JdbcTemplate dzzfJdbcTemplate;

	@SuppressWarnings("unchecked")
	public List<FundAccount> queryPersonErrorOwnerTypeInfos() {
		final String sql =
				"SELECT A.*, A.ROWID  " +
						"  FROM AC_FUNDACCOUNT A  " +
						" WHERE A.ACCOUNTSTATUS = 1  " +
						"   AND A.OWNERTYPE = 0  " +
						"   AND A.CERTIFITYPE NOT IN (0, 1, 2, 3, 4, 5, 6, 7)  " +
						"   AND EXISTS (SELECT 1  " +
						"          FROM AC_CARDINFO C  " +
						"          JOIN AC_ACCOUNT T  " +
						"            ON C.ACCOUNTID = T.ACCOUNTID  " +
						"         WHERE C.ECARDSTATUS IN (1, 2, 3)  " +
						"           AND T.FUNDACCOUNTID = A.FUNDACCOUNTID)";
		return (List<FundAccount>) dzzfJdbcTemplate.query(sql, new ListResultSetExtractor(new FundAccount()));
	}

	/**
	 * 更新卡片的绑定账户类型
	 *
	 * @param bindAccountType 绑定账户类型
	 * @param ecardNo         卡号
	 * @return 更新数量
	 */
	public int updateCardBindAccountType(int bindAccountType, String ecardNo) {
		return dzzfJdbcTemplate.update("UPDATE AC_CARDINFO A SET A.BINDACCOUNTTYPE=?  WHERE A.ECARDNO=? AND A.TOPCLASS=6109", bindAccountType, ecardNo);
	}

	/**
	 * 更新卡片相关信息
	 *
	 * @param cardBrand       卡品牌
	 * @param bindAccountType 绑定类型
	 * @param bindCardType    卡类型
	 * @param ecardNo         卡号
	 * @return 更新数量
	 */
	public int updateCardAboutInfos(int cardBrand, int bindAccountType, int bindCardType, String ecardNo) {
		return dzzfJdbcTemplate.update(" UPDATE AC_CARDINFO A SET A.CARDBRAND=?, A.BINDACCOUNTTYPE=?, A.BINDCARDTYPE=? WHERE A.ECARDNO=? AND A.TOPCLASS=6113",
				cardBrand, bindAccountType, bindCardType, ecardNo);
	}

	/**
	 * 更新证件相关信息
	 *
	 * @param ownerType   用户类型
	 * @param certifiType 证件类型
	 * @param certifiNo   证件号
	 * @param ecardNo     卡号
	 * @return 更新数量
	 */
	public int updateCertInfos(int ownerType, int certifiType, String certifiNo, String ecardNo) {
		final String sql =
				"UPDATE AC_FUNDACCOUNT A " +
						"   SET A.OWNERTYPE = ?, A.CERTIFITYPE = ?, A.CERTIFINO = ? " +
						" WHERE A.TOPCLASS = 6109 " +
						"   AND EXISTS (SELECT 1 " +
						"          FROM AC_CARDINFO C " +
						"          JOIN AC_ACCOUNT T " +
						"            ON C.ACCOUNTID = T.ACCOUNTID " +
						"         WHERE T.FUNDACCOUNTID = A.FUNDACCOUNTID " +
						"           AND C.ECARDNO = ?)";
		return dzzfJdbcTemplate.update(sql, ownerType, certifiType, certifiNo, ecardNo);
	}

	/**
	 * 更新经办人相关信息
	 */
	public int updateAgentInfos(int userIdType, String userIdNum, String agentName, String agentIdNum, int agentIdType, String tel, String ecardNo) {
		final String sql =
				"UPDATE AC_FUNDACCOUNT A\n" +
						"   SET A.AGENTIDTYPE  = ?,\n" +
						"       A.AGENTIDNUM   = ?,\n" +
						"       A.RELATIONNAME = ?,\n" +
						"       A.PHONE        = ?,\n" +
						"       A.MOBILE       = ?,\n" +
						"       A.CERTIFITYPE  = ?,\n" +
						"       A.CERTIFINO    = ?,\n" +
						"       A.OWNERTYPE    = 1\n" +
						" WHERE A.TOPCLASS = 6109\n" +
						"   AND EXISTS (SELECT 1\n" +
						"          FROM AC_CARDINFO C\n" +
						"          JOIN AC_ACCOUNT T\n" +
						"            ON C.ACCOUNTID = T.ACCOUNTID\n" +
						"         WHERE T.FUNDACCOUNTID = A.FUNDACCOUNTID\n" +
						"           AND C.ECARDNO = ?)";
		return dzzfJdbcTemplate.update(sql, agentIdType, agentIdNum, agentName, tel, tel, userIdType, userIdNum, ecardNo);
	}

}
